package alexattar.gdxtest.screens;

import alexattar.gdxtest.GdxTestGame;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;

public abstract class AbstractScreen implements Screen{
	public static final int GAME_VIEWPORT_WIDTH = 400, GAME_VIEWPORT_HEIGHT = 240;
    public static final int MENU_VIEWPORT_WIDTH = 800, MENU_VIEWPORT_HEIGHT = 480;
	
	protected final GdxTestGame game;
	protected final Stage stage;
	
	private SpriteBatch batch;
	private BitmapFont font;
	private Skin skin;
	private TextureAtlas atlas;
	private Table table;
	
	public AbstractScreen(GdxTestGame game) {
		this.game = game;
		int width = (isGameScreen() ? GAME_VIEWPORT_WIDTH : MENU_VIEWPORT_WIDTH);
		int height = (isGameScreen() ? GAME_VIEWPORT_HEIGHT : MENU_VIEWPORT_HEIGHT);
		this.stage = new Stage(width, height, true);
	}
	
	protected String getName() {
		return getClass().getSimpleName();
	}
	
	public BitmapFont getFont() {
		if (font == null) {
			font = new BitmapFont();
		}
		return font;
	}
	
	public SpriteBatch getBatch() {
		if (batch == null) {
			batch = new SpriteBatch();
		}
		return batch;
	}
	
	public TextureAtlas getAtlas() {
		if (atlas == null) {
			atlas = new TextureAtlas(Gdx.files.internal("image-atlases/pages-info.atlas"));
		}
		return atlas;
	}
	
	protected Skin getSkin() {
		if (skin == null) {
			FileHandle skinFile = Gdx.files.internal("skin/uiskin.json");
			skin = new Skin(skinFile);
		}
		return skin;
	}
	
	protected Table getTable() {
		if (table==null) {
			table = new Table(getSkin());
			table.setFillParent(true);
			stage.addActor(table);
		}
		return table;
	}
	
	@Override
	public void show() {
		Gdx.input.setInputProcessor( stage );
		Gdx.app.log(GdxTestGame.LOG, "Showing screen: " + getName());
	}
	
	@Override
	public void resize(int width, int height) {
		Gdx.app.log(GdxTestGame.LOG, "Resizing screen: " + getName() + " to: " + width + "x" + height);
	}
	
	@Override
	public void render(float delta) {
		
		// (1) Process the game logic
		stage.act(delta);
		
		// (2) Draw the result
		Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
		
		stage.draw();
	}
	
	@Override
	public void hide() {
		Gdx.app.log(GdxTestGame.LOG, "Hiding screen: " + getName());
		dispose();
	}
	
	@Override
	public void pause() {
		
	}
	
	@Override
	public void resume() {
		
	}
	
	@Override
	public void dispose() {
		Gdx.app.log(GdxTestGame.LOG, "Disposing screen: " + getName());
		getFont().dispose();
		getBatch().dispose();
	}

	protected boolean isGameScreen() {
		return false;
	}
}
